﻿using UnityEngine;
using FairyGUI;

public class FWeapon : GButton
{
    GLoader _loader;
    Weapon _weaponData;
    GLabel _discriptLabel;
    GLabel _nameLabel;
    GLabel _suitNameLabel;

    GComponent _randDefense;
    GComponent _randElement;

    public override void ConstructFromXML(FairyGUI.Utils.XML cxml)
    {
        base.ConstructFromXML(cxml);

        _loader = ((GComponent)this.GetChild("card")).GetChild("imgCom").asCom.GetChild("img").asLoader;
        _discriptLabel = this.GetChild("discriptLabel").asLabel;
        _nameLabel = this.GetChild("nameLabel").asLabel;
        _suitNameLabel = this.GetChild("suitNameLabel").asLabel;

        _randElement = this.GetChild("randElement").asCom;
        _randDefense = this.GetChild("randDefense").asCom;
    }

    public void InitData(Weapon weaponData,bool isShowClickAni = true)
    {
        _loader.parent.touchable = true;
        if (!isShowClickAni) _loader.parent.touchable = false;
        _weaponData = weaponData;
        Fresh();
    }

    public void Fresh(int level = 0)
    {
        if (_weaponData == null) return;

        _randElement.visible = false;
        _randDefense.visible = false;
        if(_weaponData.type == GMConfig.WeaponType.Weapon)
        {
            if (_weaponData.randProperties[0].isRand == 1) {
                _randDefense.text = "随机防抗 " + _weaponData.randProperties[0].num;
                _randDefense.visible = true;
            }
            if (_weaponData.randProperties[1].isRand == 1) {
                 _randElement.text = "随机元素 " + _weaponData.randProperties[1].num;
                _randElement.visible = true;
            }
        }
        else
        {
            if (_weaponData.randProperties[0].isRand == 1) {
                _randDefense.text = "随机属性 " + _weaponData.randProperties[0].num;
                _randDefense.visible = true;
            }
        }

        _loader.url = _weaponData.img;
        var suitList = _weaponData.GetSuits();
        var suitName = "";
        foreach(var item in suitList){
            if(suitName != "") suitName += ",";
            suitName += item.GetSuitName();
        }
        if(suitName != "") suitName = "套装：" + suitName;
        _suitNameLabel.text = suitName;

        var name = _weaponData.GetWeaponName();
        if (_nameLabel.text != name) _nameLabel.text = name;
        //默认展示1级
        var discript = _weaponData.GetPropertyDiscript(level);
        if (_discriptLabel.text != discript) _discriptLabel.text = discript;
    }

    public void SetImg(string url)
    {
        _loader.url = url;
    }

    public string GetCardImg()
    {
        return _loader.url;
    }
}

